package com.example.mywiseelves.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.mywiseelves.bean.ResourcesBean;
import com.example.mywiseelves.bean.VoiceBean;
import com.example.mywiseelves.utils.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZJK
 * @data 2021 10
 * @description
 */
public class VoicesDao {
    private final DBHelper dbHelper;

    public VoicesDao(Context context) {
        dbHelper = new DBHelper(context);
    }

    /**
     * 添加数据，添加初始语音资源
     */
    public void insertData(int voice_id, String voice) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //添加数据
            //语音id
            values.put("voice_id", voice_id);
            //语音资源
            values.put("voice", voice);
            //插入数据
            db.insert("voices", null, values);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }
    /**
     * 查询所有数据，用于插入时查询
     */
    public List<VoiceBean> allVoiceQuery() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        List<VoiceBean> voiceBeanArrayList = new ArrayList<>();
        VoiceBean voiceBean ;
        try {
            //开启事务
            db.beginTransaction();
            String sql = "select * from voices";
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                voiceBean = new VoiceBean();
                voiceBean.setVoice_id(cursor.getInt(cursor.getColumnIndex("voice_id")));
                voiceBean.setVoice(cursor.getString(cursor.getColumnIndex("voice")));
                voiceBeanArrayList.add(voiceBean);
            }
            cursor.close();
            db.setTransactionSuccessful();
            Log.v("myapp", "查询成功,一共：" + voiceBeanArrayList.size() + "条数据");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
        return voiceBeanArrayList;
    }
    /**
     * 查询数据，用于语音识别
     */
    public String voicesQuery(int voice_id) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String voice = "";
        try {
            //开启事务
            db.beginTransaction();
            String sql = "select voice from voices where voice_id = "+voice_id;
            Cursor cursor = db.rawQuery(sql, null);
            // 看看查询的结果是否为空
            if (cursor == null) {
                return "";
            }
            while (cursor.moveToNext()) {
                voice =cursor.getString(cursor.getColumnIndex("voice"));
            }
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
        return voice;
    }

    /**
     * 信息更新，更新语音资源，在设置页面
     */
    public void voicesUpdate(int voice_id,String voice) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            String[] args = {String.valueOf(voice_id)};
            //存储体
            ContentValues values = new ContentValues();
            //图片状态状态
            values.put("voice", voice);
            //更新数据
            db.update("voices", values, "voice_id = ?", args);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

}
